package mysql

import (
	"gorm.io/gorm"
	"practice-one/userrpc/userrpc"
)

type Comment struct {
	gorm.Model
	UserId   int64  `gorm:"type:int(11);not null;comment:用户id"`
	GoodsId  int64  `gorm:"type:int(11);not null;comment:商品id"`
	Comments string `gorm:"type:varchar(500);not null;comment:评论"`
}

func (c *Comment) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

/*
SELECT

	users.nick_name,
	users.head_image,
	comments.comments

FROM

	comments
	LEFT JOIN users ON comments.user_id = users.id

WHERE comments.goods_id =2
*/
func CommentList(db *gorm.DB, id int64) []*userrpc.CommentList {
	var list []*userrpc.CommentList
	db.Debug().Model(&Comment{}).
		Select(
			"users.nick_name",
			"users.head_image",
			"comments.comments",
		).
		Joins("LEFT JOIN users ON comments.user_id = users.id").
		Where("comments.goods_id =?", id).
		Order("comments.created_at").
		Find(&list)
	return list
}
